package com.stationbuilding.fragment.member;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.stationbuilding.R;
import com.stationbuilding.activity.member.MemberDetailsActivity;
import com.stationbuilding.adapter.home.HomeListAdapter;
import com.stationbuilding.adapter.member.MemberFragmentPagerAdapter;
import com.stationbuilding.adapter.member.MemberListAdapter;
import com.stationbuilding.bean.MemberBean;
import com.stationbuilding.bean.QuestionBean;
import com.stationbuilding.fragment.safe.SafeFragmentLevel;
import com.stationbuilding.fragment.safe.SafeFragmentScore;
import com.stationbuilding.fragment.safe.SafeFragmentStatus;
import com.stationbuilding.utils.PinyinComparator;
import com.stationbuilding.utils.PinyinUtil;
import com.stationbuilding.utils.PreferenceHelper;
import com.stationbuilding.utils.TabLayoutUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MemberFragmentCompany extends Fragment {

    private View view;
    private ListView listView;
    private MemberListAdapter memberListAdapter;
    private List<MemberBean> memberList;
    private boolean checkModal;
    private ArrayList<String> userIdList = new ArrayList<>();
    private ArrayList<String> userNameList = new ArrayList<>();

    public MemberFragmentCompany(boolean checkModal) {
        this.checkModal = checkModal;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_member_member, container, false);
        initViews();
        initEvents();
        return view;
    }

    private void initViews() {
        listView = (ListView) view.findViewById(R.id.member_list);
        setAdapter();
    }

    private void setAdapter() {
        memberList = filledData(getResources().getStringArray(R.array.contacts));
        Collections.sort(memberList, new PinyinComparator());
        memberListAdapter = new MemberListAdapter(getActivity(), memberList, checkModal);
        listView.setAdapter(memberListAdapter);
    }

    private void initEvents() {
        // ListView的点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (checkModal) {
                    String userId = ((MemberBean) memberListAdapter.getItem(position)).getUserId();
                    if (userIdList.contains(userId)) {
                        int index = userIdList.indexOf(userId);
                        userIdList.remove(index);
                        userNameList.remove(index);
                        ((MemberBean) memberListAdapter.getItem(position)).setChecked(false);
                    } else {
                        userIdList.add(userId);
                        userNameList.add(((MemberBean) memberListAdapter.getItem(position)).getUsername());
                        ((MemberBean) memberListAdapter.getItem(position)).setChecked(true);
                    }
                    memberListAdapter.notifyDataSetChanged();
                } else {
                    getActivity().startActivity(new Intent(getActivity(), MemberDetailsActivity.class));
                }
            }
        });
    }

    /**
     * 根据输入框中的值来过滤数据并更新ListView
     *
     * @param filterStr
     */
    public void filterData(String filterStr) {
        List<MemberBean> sortList = new ArrayList<>();
        if (TextUtils.isEmpty(filterStr)) {
            sortList = memberList;
        } else {
            sortList.clear();
            for (MemberBean memberBean : memberList) {
                String name = memberBean.getUsername();
                if (name.toUpperCase().indexOf(filterStr.toString().toUpperCase()) != -1 || PinyinUtil.getPingYin(name).toUpperCase().startsWith(filterStr.toString().toUpperCase())) {
                    sortList.add(memberBean);
                }
            }
        }
        // 根据a-z进行排序
        Collections.sort(sortList, new PinyinComparator());
        memberListAdapter.updateListView(sortList);
    }

    private List<MemberBean> filledData(String[] data) {
        List<MemberBean> sortList = new ArrayList<>();
        ArrayList<String> indexString = new ArrayList<>();

        for (int i = 0; i < data.length; i++) {
            MemberBean memberBean = new MemberBean();
            memberBean.setUsername(data[i]);
            String pinyin = PinyinUtil.getPingYin(data[i]);
            String sortString = pinyin.substring(0, 1).toUpperCase();
            if (sortString.matches("[A-Z]")) {
                memberBean.setSortLetters(sortString.toUpperCase());
                if (!indexString.contains(sortString)) {
                    indexString.add(sortString);
                }
            }
            sortList.add(memberBean);
        }
        Collections.sort(indexString);
        return sortList;
    }

    /**
     * 设置选择结果
     */
    public void setResult() {
        if (userIdList == null || userIdList.size() == 0) {
            Toast.makeText(getActivity(), "请选择联系人！", Toast.LENGTH_SHORT).show();
            return;
        }
        Intent intent = new Intent();
        intent.putStringArrayListExtra("id", userIdList);
        intent.putExtra("name", userNameList);
        getActivity().setResult(Activity.RESULT_OK, intent);
        getActivity().finish();
    }
}
